﻿Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Text

Public Class PTIC_Button
    Inherits Windows.Forms.Button

#Region "Appels API"
    Public Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As IntPtr, ByVal hRgn As IntPtr, ByVal bRedraw As Boolean) As IntPtr
#End Region

#Region "Enumérations"
    Public Enum nomThemeButton
        OMANE
        OMANESuppr
        OMANEDescr
        OMANEBackColor
        Standard
    End Enum
#End Region

#Region "Variables privées"
    Private _theme As ThemeComposant
    Private _nomTheme As nomThemeButton
#End Region

#Region "Création d'une instance"
    Public Sub New()
        'Définition des styles d'affichage pour éviter le flicking
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.Opaque, True)
        SetStyle(ControlStyles.ResizeRedraw, True)
        'Définition du style à appliquer au bouton
        nomStyle = nomThemeButton.OMANE
    End Sub
#End Region

#Region "Propriétés"

    <Browsable(True), Category("Thème")> _
    Public Property nomStyle() As nomThemeButton

        Get
            Return _nomTheme
        End Get

        Set(ByVal value As nomThemeButton)

            Dim etat As etatComposant
            If Me.Enabled Then etat = etatComposant.Enabled Else etat = etatComposant.Disabled

            _nomTheme = value

            If value = nomThemeButton.OMANE or value=nomThemeButton.OMANEBackColor or _
               value=nomThemeButton.OMANEDescr or value=nomThemeButton.OMANESuppr Then

                Dim styleOMANEbtnSTD As _
                        New styleEtatComposant(My.Settings.OMANEBtnSTD_ColorDegrade, _
                                               My.Settings.OMANEBtnSTD_Font, _
                                               My.Settings.OMANEBtnSTD_ColorFore, _
                                               My.Settings.OMANEBtnSTD_ColorBorder, _
                                               My.Settings.OMANE_AngleRoundRect)
                Dim styleOMANEbtnMOUSEOVER As _
                        New styleEtatComposant(My.Settings.OMANEBtnMOUSEOVER_ColorDegrade, _
                                               My.Settings.OMANEBtnMOUSEOVER_Font, _
                                               My.Settings.OMANEBtnMOUSEOVER_ColorFore, _
                                               My.Settings.OMANEBtnMOUSEOVER_ColorBorder, _
                                               My.Settings.OMANE_AngleRoundRect)
                Dim styleOMANEbtnDIS As _
                        New styleEtatComposant(My.Settings.OMANEBtnDIS_ColorDegrade, _
                                               My.Settings.OMANEBtnDIS_Font, _
                                               My.Settings.OMANEBtnDIS_ColorFore, _
                                               My.Settings.OMANEBtnDIS_ColorBorder, _
                                               My.Settings.OMANE_AngleRoundRect)

                _theme = New ThemeComposant(styleOMANEbtnSTD, _
                                            styleOMANEbtnMOUSEOVER, _
                                            styleOMANEbtnDIS, etat)

                If _nomTheme = nomThemeButton.OMANEBackColor Then
                    _theme.styleNormal.backColor = Me.BackColor
                    _theme.styleNormal.borderColor = Me.BackColor
                    _theme.styleNormal.foreColor = My.Settings.OMANE_STDForeColor
                    _theme.styleMouseOver.backColor = Me.BackColor
                    _theme.styleMouseOver.borderColor = Me.BackColor
                    _theme.styleNormal.foreColor = My.Settings.OMANE_MOUSEOVERForeColor
                End If

                If _nomTheme = nomThemeButton.OMANEDescr Then
                    _theme.styleNormal.backColor = My.Settings.OMANEBtnDescr_ColorDegrade
                    _theme.styleNormal.borderColor = My.Settings.OMANEBtnDescr_ColorDegrade
                End If

                If _nomTheme = nomThemeButton.OMANESuppr Then
                    _theme.styleNormal.backColor = My.Settings.OMANEBtnSuppr_ColorDegrade
                    _theme.styleNormal.borderColor = My.Settings.OMANEBtnSuppr_ColorDegrade
                End If

            End If

            Invalidate()

        End Set

    End Property

#End Region

#Region "Redéfinition de la méthode Paint"

    Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)

        If _nomTheme = nomThemeButton.OMANE Or _nomTheme = nomThemeButton.OMANESuppr Or _
           _nomTheme = nomThemeButton.OMANEBackColor Or _nomTheme = nomThemeButton.OMANEDescr Then

            'Récupération du style en cours
            Dim styleEnCours As styleEtatComposant = _theme.styleEnCours

            'Récupération du graphique associé à la méthode paint
            Dim g As Graphics = pevent.Graphics
            'Nettoyage de l'objet graphique
            g.Clear(Color.White)
            'Mode de dessin
            'g.InterpolationMode = InterpolationMode.HighQualityBilinear
            g.SmoothingMode = SmoothingMode.AntiAlias


            'Définition de la zone de dégradé (décalage de 2 pour bordure blanche et bordure bleue)
            Dim rectDegrade As New Rectangle(Me.ClientRectangle.X + 2, _
                                            Me.ClientRectangle.Y + 2, _
                                            Me.ClientRectangle.Width - 4, _
                                            Me.ClientRectangle.Height - 4)
            Dim GPathDegrade As GraphicsPath = Styles.GetRoundRectangle(rectDegrade, _
                                                                        styleEnCours.angleRoundRect, _
                                                                        styleEnCours.angleRoundRect, _
                                                                        styleEnCours.angleRoundRect, _
                                                                        styleEnCours.angleRoundRect)

            Styles.PaintBackAeroGlassOMANE(g, rectDegrade, styleEnCours.backColor, GPathDegrade)

            'Dessin de l'icone éventuelle
            If Not (Me.Image Is Nothing) Then
                '----- Redimensionnement
                'nouveau size pour picturebox2
                'Dim grunit As GraphicsUnit = GraphicsUnit.Display
                'initTailleImage()
                'Calcul la position de l'image en fonction de sa taille
                g.DrawImage(Me.Image, CInt((Me.Height - Me.Image.Height) / 2) + 4, CInt((Me.Height - Me.Image.Height) / 2) + 1)
            End If

            'Tracé du texte
            Dim SizeTxt As Size = TextRenderer.MeasureText(Me.Text, styleEnCours.font)
            If Not Me.Image Is Nothing Then
                g.DrawString(Me.Text, styleEnCours.font, New SolidBrush(styleEnCours.foreColor), CInt((Me.Height - Me.Image.Height) / 2) * 2 + Me.Image.Width + 3, (Me.Height - SizeTxt.Height) / 2)
            Else
                g.DrawString(Me.Text, styleEnCours.font, New SolidBrush(styleEnCours.foreColor), 6, (Me.Height - SizeTxt.Height) / 2)
            End If

            'Définition de la zone de dessin du bouton (hors bordures)
            Dim rectBords As New Rectangle(Me.ClientRectangle.X, _
                                            Me.ClientRectangle.Y, _
                                            Me.ClientRectangle.Width - 1, _
                                            Me.ClientRectangle.Height - 1)
            Dim GPathRectBords As GraphicsPath = Styles.GetRoundRectangle(rectBords, _
                                                                          styleEnCours.angleRoundRect, _
                                                                          styleEnCours.angleRoundRect, _
                                                                          styleEnCours.angleRoundRect, _
                                                                          styleEnCours.angleRoundRect)
            g.DrawPath(New Pen(styleEnCours.borderColor, 1), GPathRectBords)

            ''g.DrawPath(Styles.Instance.PenBorders, GPath)
            ''Dim newRegion As New Region(GPath)

            ''Dim path2 As GraphicsPath = GPath.Clone
            ''path2.Widen(New Pen(Color.Blue, 1))
            ''newRegion.Union(path2)

            'SetWindowRgn(Me.Handle, newRegion.GetHrgn(g), True)

        Else
            MyBase.OnPaint(pevent)
        End If

    End Sub

#End Region

#Region "Interception des évènements"

    Private Sub PTIC_Button_BackColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.BackColorChanged
        If _nomTheme = nomThemeButton.OMANEBackColor Then
            _theme.styleNormal.backColor = Me.BackColor
            _theme.styleNormal.borderColor = Me.BackColor
            _theme.styleMouseOver.backColor = Me.BackColor
            _theme.styleMouseOver.borderColor = Me.BackColor
        End If
        Invalidate()
    End Sub

    Private Sub PTIC_ButtonOMANE_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.EnabledChanged
        If Me.Enabled Then
            _theme.etat = etatComposant.Enabled
        Else
            _theme.etat = etatComposant.Disabled
        End If
        Me.Invalidate()
    End Sub

    Private Sub PTIC_ButtonOMANE_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
        _theme.etat = etatComposant.MouseOver
    End Sub

    Private Sub PTIC_ButtonOMANE_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
        _theme.etat = etatComposant.Enabled
    End Sub

#End Region

End Class
